package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.service.BidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoji on 2014/11/6.
 */
@Controller
@RequestMapping("/bid")
public class BidController {
    @Autowired
    private BidService bidService;
    @RequestMapping(value = "/getTaskInfoByTaskId", method = RequestMethod.GET)
    public String getTaskInfoByTaskId(Model model,Integer taskId){
        Map<String,Object> map = bidService.getTaskInfoByTaskId(taskId);
        model.addAttribute("taskinfo",map);
        return "/bid/addbid";
    }
    @RequestMapping(value = "/addBidInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addBidInfo(Integer taskId,String selfDescription,String attachment,
                                          BigDecimal budget,Integer cycle,HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        if(budget == null || cycle == null || selfDescription == null){//任务报价、任务工期、任务描述不为空
            map.put("status",-2);
            return map;
        }
        map.put("itemId",taskId);
        map.put("artistId",userInfo.getArtistId());
        map.put("userId",userInfo.getUserId());
        map.put("attachment",attachment);//附件
        map.put("planDescription",selfDescription);
        map.put("budget",budget);
        map.put("cycle",cycle);
        String pieceNumObj = request.getParameter("pieceNum");
        //纪检数量
        if(pieceNumObj != null){
            map.put("pieceNum",Integer.parseInt(pieceNumObj));
        }

        Map<String, Object> result = bidService.addBidInfo(map);
        return result;
    }
}
